package cn.com.czpb.client.module.news.utils;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import cn.com.czpb.client.R;
import cn.com.czpb.client.model.news.NewsTagBean;
import cn.com.czpb.client.model.news.NewsTagEntity;
import cn.com.czpb.client.module.news.adapter.NewsCancelGridAdapter;
import cn.com.czpb.client.module.news.dialog.FitPopupWindow;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DensityUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ScreenUtils;
import cn.com.czpb.client.utils.TUtils;

import java.util.HashMap;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 * Created by DongJr on 2017/2/27.
 */

public class FitPopupUtil implements AdapterView.OnItemClickListener {

    private View contentView;

    private Activity context;

    private TextView btnCommit;


    private FitPopupWindow mPopupWindow;

    private OnCommitClickListener listener;
    private final GridView gridView;
    private String nid;
    private final NewsCancelGridAdapter adapter;
    private final HashMap<Integer, String> map;
    private View anchorView;

    public FitPopupUtil(Activity context, String nid) {
        this.nid = nid;
        this.context = context;
        map = new HashMap<>();
        LayoutInflater inflater = LayoutInflater.from(context);
        contentView = inflater.inflate(R.layout.layout_popupwindow, null);
        gridView = contentView.findViewById(R.id.gv_dislikelist_id);
        btnCommit = contentView.findViewById(R.id.btn_commit);
        adapter = new NewsCancelGridAdapter(context);
        gridView.setAdapter(adapter);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        gridView.setOnItemClickListener(this);
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TUtils.toast("请选择理由");
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        TextView button = view.findViewById(R.id.btn_pop_item_id);
        NewsTagBean bean = (NewsTagBean) adapter.getItem(position);
        adapter.choiceState(position);
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(adapter.getTaglist());
                if (mPopupWindow != null) {
                    mPopupWindow.dismiss();
                }
            }
        });
    }

    private void getNewsTagList() {
        LogUtils.i("nid-->" + nid);
        Map<String, String> map = new HashMap();
        map.put("nid", nid);
        Factory.provideHttpService().newsLoseInterestTags(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NewsTagEntity>() {
                    @Override
                    public void call(NewsTagEntity entity) {
                        adapter.appendData(entity.data.getTags(), true);
                        adapter.notifyDataSetChanged();
                        showPopup();
                        LogUtils.i("nid-->size-->" + entity.data.getTags().size());
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("异常-->" + throwable.toString());
                    }
                });
    }

    public void setOnClickListener(OnCommitClickListener listener) {
        this.listener = listener;
    }

    /**
     * 弹出自适应位置的popupwindow
     *
     * @param
     */
    public View showPopup() {
        if (mPopupWindow == null) {
            mPopupWindow = new FitPopupWindow(context,
                    ScreenUtils.getScreenWidth(context) - DensityUtils.dp2px(20),
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
        }

        mPopupWindow.setView(contentView, anchorView);
        mPopupWindow.show();
        return contentView;
    }

    public void getData(View anchorView) {
        this.anchorView = anchorView;
        getNewsTagList();
    }


    public interface OnCommitClickListener {
        void onClick(String reason);
    }

}
